package com.buric.auth.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.buric.auth.service.CommonService;
import com.buric.common.sevice.RedisService;
import com.wf.captcha.SpecCaptcha;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 通用工具Service实现
 * </p>
 *
 * @author zhou.wei
 * @version 1.0
 * @since 2025/2/10 13:43
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    private RedisService redisService;

    @Override
    public String getCaptcha(String captchaId, Long expireTime) {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        String verCode = specCaptcha.text().toLowerCase();
        // 存入redis并设置过期时间为30分钟
        redisService.setCacheObject(captchaId, verCode, expireTime, TimeUnit.SECONDS);
        // 将base64返回给前端
        return specCaptcha.toBase64();
    }

    @Override
    public boolean checkCaptcha(String captchaId, String captcha) {
        String value = redisService.getCacheObject(captchaId);
        if(ObjectUtil.isEmpty(value)){
            return false;
        }
        if(value.equals(captcha)){
            return true;
        }
        return false;
    }


}
